using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ZeroSum.Constants;

namespace ZeroSum.App.Extensions;

/// <summary>
///     数据生成扩展
/// </summary>
public static class DataProviderExtension
{
    /// <summary>
    ///     定义初始化数据路由（自动执行所有的IDataProvider
    /// </summary>
    /// <param name="app"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseSeedData(this IApplicationBuilder app, string path = "/init")
    {
        app.UseEndpoints(
            endpoint =>
            {
                endpoint.MapGet(
                        path, async ([FromServices] IEnumerable<IDataSeedProvider> provider) =>
                        {
                            var orderedProvider = provider.OrderBy(x => x.Order).ToList();
                            foreach (var seedProvider in orderedProvider) await seedProvider.GenerateAsync();

                            return "数据生成完成";
                        }
                    )
                    .ExcludeFromDescription(); // TODO: 无法在swagger里面显示，暂时排除在swagger中的显示。
            });
        return app;
    }
}